<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="姓名" prop="workerName">
        <el-input
          v-model="queryParams.workerName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名日期" prop="registrationTime">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>

      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          size="small"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="银行卡号" prop="bankCardNumber">
        <el-input
          v-model="queryParams.bankCardNumber"
          placeholder="请输入银行卡号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
        <el-form-item label="黑名单" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="黑名单"
            clearable
            size="small"
            style="width: 240px"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form-item>

      <el-form-item label="手机" prop="phoneCode">
        <el-input
          v-model="queryParams.phoneCode"
          placeholder="请输入手机号"
          clearable
          size="small"
          style="width: 240px"
        />
      </el-form-item>


      <el-form-item label="供应商" prop="companyId" v-hasPermi="['job:company:select']">
        <el-select
          v-model="queryParams.companyId"
          placeholder="供应商"
          clearable
          size="small"
          style="width: 240px"

        >
          <el-option
            v-for="dict in companyOptions"
            :key="dict.id"
            :label="dict.company"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="班次" prop="timeShiftType">
        <el-select
          v-model="queryParams.timeShiftType"
          placeholder="班次"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in timShiftOptions"
            :key="dict.type"
            :label="dict.name"
            :value="dict.type"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="工作状态" prop="workStatus">
        <el-select
          v-model="queryParams.workStatus"
          placeholder="工作状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="item in workStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="partTimeJobs" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="员工编号" prop="id" width="80"/>
      <el-table-column label="员工姓名" prop="workerName" :show-overflow-tooltip="true" width="100"/>
      <el-table-column label="性别" prop="sexStr" :show-overflow-tooltip="true" width="80"/>
      <el-table-column label="手机号" prop="phoneCode" width="130"/>
      <el-table-column label="身份证" prop="idCard" width="180"/>
      <el-table-column label="班次" prop="timeShiftName" width="100"/>
      <el-table-column label="供应商" prop="company" width="100"/>
      <el-table-column label="报名时间" align="center" prop="registrationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registrationTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上班签到时间" align="center" prop="signInTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signInTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下班签到时间" align="center" prop="offWorkTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.offWorkTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="银行卡号" prop="bankCardNumber" width="100"/>
      <el-table-column label="银行开户行" prop="bankAddress" width="100"/>
      <el-table-column label="黑名单" align="center" width="100">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope" >
          <el-button
            v-if="scope.row.workStatus =='2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateWork(scope.row)"
            v-hasPermi="['job:company:singWork']"
          >手动签退
          </el-button>
          <el-button
            v-if="scope.row.workStatus == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateWork(scope.row)"
            v-hasPermi="['job:company:singWork']"
          >手动签到
          </el-button>
          <el-link type="primary" @click="handOpenImage(scope.row.id)">链接</el-link>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <div class="block">
        <span class="demonstration">省份证正面照片</span>
        <el-image :src="imgSrc"></el-image>
      </div>
      <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
            </span>
    </el-dialog>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {
  listWorkerInfos,
  listCompany,
  listTimShift,
  exportWorkerInfos,
  changeWorkerStatus,
  changeWorkStatus,
  getImg
} from "@/api/partTimeJob/workerInfo/workerInfo";

export default {
  name: "worker",
  data() {
    return {
      dialogVisible:false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      partTimeJobs: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 黑名单下拉
      statusOptions: [{value: 1, label: '是'}, {value: 2, label: '否'}],
      workStatusOptions: [{value: 1, label: '已报名'}, {value: 2, label: '已签到'}, {value: 3, label: '候选'}, {
        value: 4,
        label: '已取消候选'
      }, {value: 5, label: '已取消报名'}, {value: 6, label: '已完成'}],
      //用人单位下拉
      companyOptions: [],
      //班次下拉
      timShiftOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        //员工姓名
        workerName: undefined,
        //身份证
        idCard: undefined,
        //银行卡
        bankCardNumber: undefined,
        //黑名单
        status: undefined,
        //手机号码
        phoneCode: undefined,
        //供应商
        company: undefined,
        //班次
        timShiftTYpe: undefined,
        companyId: undefined,
        timeShiftType: undefined,
        workStatus: 6,

      },
      imgSrc:'',
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      // rules: {
      //   workerName: [
      //     { required: true, message: "员工姓名不能为空", trigger: "blur" }
      //   ],
      //   phoneCode: [
      //     { required: true, message: "手机号不能为空", trigger: "blur" }
      //   ],
      //   idCard: [
      //     { required: true, message: "省份证不能为空", trigger: "blur" }
      //   ]
      // }
    };
  },
  created() {
    this.getTime();
    this.getList();
    this.getCompanyList();
    this.listTimShift();
  },
  methods: {
    getTime () {
      let _this = this;
      let date = new Date();
      let y = date.getFullYear();
      let m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1);
      let d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate();

      let start_month = y + '-' + '01';   //开始月份：本年第一月份
      let end_month = `${y}-${m}`;        //结束月份： 本年当前月份

      let start_day = `${y}-${m}-${d}`;       //开始日期： 本月第一天
      let end_day = `${y}-${m}-${d}`;     //结束日期： 本月今天

      //_this.aa = [start_month, end_month];   //默认月份区间
      _this.dateRange = [start_day, end_day];        //默认日期区间

    },
    /** 获取员工信息列表 */
    getList() {
      this.loading = true;
      listWorkerInfos(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.partTimeJobs = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /**获取供应商列表**/
    getCompanyList() {
      listCompany({status: 1}).then(
        response => {
          this.companyOptions = response.data;
        }
      )
    },
    //黑名单
    handleStatusChange(row) {
      let text = row.status == "0" ? "停用" : "启用";
      this.$confirm('确认要"' + text + '""' + row.workerName + '"吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeWorkerStatus(row.id, row.status);
      }).then(() => {
        this.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    /**班次下拉**/
    listTimShift() {
      listTimShift({status: 1}).then(
        response => {
          this.timShiftOptions = response.data;
        }
      )
    },

    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
        this.menuNodeAll = false,
        this.deptExpand = true,
        this.deptNodeAll = false,
        this.form = {
          //员工姓名
          workerName: undefined,
          //身份证
          idCard: undefined,
          //银行卡
          bankCardNumber: undefined,
          //黑名单
          //手机号码
          phoneCode: undefined,
          //供应商
          company: undefined,
          //班次
          timShiftTYpe: undefined,
          companyId: undefined,
          timeShiftType: undefined,
          status: "0",
          menuIds: [],
          deptIds: [],
          menuCheckStrictly: true,
          deptCheckStrictly: true,
          remark: undefined
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },


    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有员工数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportWorkerInfos(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },

    handleUpdateWork(param){
     let workStatus =  param.workStatus == 1?'签到':param.workStatus == 2?'签退':'';
      let text = param.workerName
      let status = param.workStatus == 1? 2:param.workStatus == 2?6:0;
      changeWorkStatus(param.orderCode,status).then(res=>{
        this.msgSuccess( text+ workStatus+"成功");
      }).catch(()=>{
        this.msgError(text+ workStatus+"失败");
      })
    },
   handOpenImage(workerId) {
     this.dialogVisible = true
      getImg(workerId).then(res=>{
        this.imgSrc = res.data;
      })
    }
  }
};
</script>
